package com.chuangjiangx.member.coupon.ddd.domain.repository;


import com.chuangjiangx.dddbase.Repository;
import com.chuangjiangx.member.coupon.ddd.domain.model.*;
import com.chuangjiangx.member.basic.dao.mapper.InMbrConfigMapper;
import com.chuangjiangx.member.basic.dao.model.InMbrConfig;
import com.chuangjiangx.member.basic.dao.model.InMbrConfigExample;
import com.chuangjiangx.member.basic.ddd.domain.model.CustomRecharge;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author LeeXyan
 * @Date 2018/1/19 18:07
 **/
@Component
public class MbrConfigRepository implements Repository<MbrConfig, MbrConfigId> {
    @Autowired
    private InMbrConfigMapper inMbrConfigMapper;

    @Override
    public MbrConfig fromId(MbrConfigId mbrConfigId) {
        InMbrConfig in = inMbrConfigMapper.selectByPrimaryKey(mbrConfigId.getId());
        return wrap(in);
    }

    @Override
    public void update(MbrConfig mbrConfig) {
        InMbrConfig in = unbox(mbrConfig);
        inMbrConfigMapper.updateByPrimaryKeySelective(in);
    }

    @Override
    public void save(MbrConfig mbrConfig) {
        InMbrConfig in = unbox(mbrConfig);
        inMbrConfigMapper.insertSelective(in);
        mbrConfig.setId(new MbrConfigId(in.getId()));
    }


    public MbrConfig findByMerchantId(MerchantId merchantId) {

        InMbrConfigExample example = new InMbrConfigExample();
        example.createCriteria().andMerchantIdEqualTo(merchantId.getId());
        List<InMbrConfig> list = inMbrConfigMapper.selectByExample(example);
        return list.isEmpty() ? null : wrap(list.get(0));
    }

    public InMbrConfig unbox(MbrConfig mbrConfig){
        InMbrConfig in = new InMbrConfig();
        in.setId(null == mbrConfig.getId() ? null : mbrConfig.getId().getId());
        in.setCustomRecharge(null == mbrConfig.getCustomRecharge() ? null : mbrConfig.getCustomRecharge().value);
        in.setSubscribePnGiftScore(mbrConfig.getSubscribePnGiftScore());
        in.setCardConsumerGrantScore(mbrConfig.getCardConsumerGrantScore());
        in.setUpdateTime(mbrConfig.getTimestamp().getUpdateTime());
        in.setCreateTime(mbrConfig.getTimestamp().getCreateTime());
        in.setMerchantId(mbrConfig.getMerchantId().getId());
        in.setCardCoverChoice(null == mbrConfig.getCardCoverChoice() ? null : mbrConfig.getCardCoverChoice().code);
        in.setCardColour(mbrConfig.getCardColour());
        in.setCardPicture(mbrConfig.getCardPicture());
        in.setCardName(mbrConfig.getCardName());
        in.setContactNumber(mbrConfig.getContactNumber());
        in.setCardPrivilegeExplain(mbrConfig.getCardPrivilegeExplain());
        in.setCardUseNotice(mbrConfig.getCardUseNotice());
        in.setGiftScore(mbrConfig.getGiftScore());
        in.setGiftMbrCouponId(null == mbrConfig.getGiftMbrCouponId() ? null : mbrConfig.getGiftMbrCouponId().getId());
        in.setWxSyncSwitch(null == mbrConfig.getWxSyncSwitch() ? null : mbrConfig.getWxSyncSwitch().code);

        in.setStoredFunc(mbrConfig.getStoredFunc());
        return in;
    }


    public MbrConfig wrap(InMbrConfig in){
        MbrConfig m = new MbrConfig(
                new MbrConfigId(in.getId()),
                in.getMerchantId()==null?null:new MerchantId(in.getMerchantId()),
                in.getCustomRecharge()==null?null:CustomRecharge.getCustomRecharge(in.getCustomRecharge()),
                in.getSubscribePnGiftScore(),
                in.getCardConsumerGrantScore(),
                in.getCardCoverChoice()==null?null:CardCoverEnum.fromCode(in.getCardCoverChoice()),
                in.getCardColour(),
                in.getCardPicture(),
                in.getCardName(),
                in.getContactNumber(),
                in.getCardPrivilegeExplain(),
                in.getCardUseNotice(),
                in.getGiftScore(),
                in.getGiftMbrCouponId()==null?null:new MbrCouponId(in.getGiftMbrCouponId()),
                in.getWxSyncSwitch()==null?null:SwicthEnum.fromCode(in.getWxSyncSwitch()),
                in.getCreateTime(),
                in.getUpdateTime()
        );
        return m;
    }
}
